import paho.mqtt.client as mqtt

# 定义回调函数，用于处理接收到的消息
def on_message(client, userdata, msg):
    print("Received message:", msg.topic, msg.payload.decode())
# 创建MQTT客户端实例
client = mqtt.Client()

# 配置MQTT服务器地址和端口
broker_address = "mqtt://localhost:9999"
client.connect(broker_address)

# 设置订阅回调函数
client.on_message = on_message

# 订阅主题
topic = "0629tt"
client.subscribe(topic)

try:
    # 启动MQTT客户端循环
    client.loop_forever()
except KeyboardInterrupt:
    # 捕捉键盘中断信号
    pass

# 停止MQTT客户端循环和断开连接
client.loop_stop()
client.disconnect()